Typical HAL I2C use cases for migration

This section provides information about typical HAL I2C use cases for migration.

Use case: I2C communication in polling mode

This use case shows how to initialize an I2C instance and start a master transmit operation in polling mode.

Note

The API for all communication modes was renamed between HAL1 and HAL2.

Sequence diagram

    @startuml
    participant "Application" as App
    participant "I2C" as I2C

    App -> I2C: HAL_I2C_Init()
    App -> I2C: HAL_I2CEx_ConfigAnalogFilter()
    App -> I2C: HAL_I2C_Master_Transmit()

   @enduml

Code snippet

HAL1

HAL2

/* Add your init and cpnfig */
HAL_I2C_Init(&hi2c);
HAL_I2CEx_ConfigAnalogFilter(&hi2c,
                             I2C_ANALOGFILTER_ENABLE) ;
/* Start a I2C Process */
HAL_I2C_Master_Transmit(&hi2c,
                        DevAddress,
                        &pData,
                        Size,
                        Timeout);
HAL_I2C_Init(&hi2c, HAL_I2C1);
HAL_I2C_SetConfig(&hi2c, &i2c_config);
HAL_I2C_EnableAnalogFilter(&hi2c);
/* Start a I2C Process */
HAL_I2C_MASTER_Transmit(&hi2c,
                        device_addr,
                        &p_data,
                         size_byte,
                         timeout_ms);

Key changes

  1. HAL2 uses renamed I2C APIs (for example, HAL_I2C_MASTER_Transmit() instead of HAL_I2C_Master_Transmit()).

  2. In HAL2, the I2C configuration is applied via HAL_I2C_SetConfig().

  3. In HAL2, the analog filter is enabled via HAL_I2C_EnableAnalogFilter().

Use case: I2C communication in DMA mode

This use case shows how to handle master and slave transmit/receive using DMA.

Sequence diagram

@startuml
  participant "User Application" as App
  participant "HAL I2C" as HAL_I2C


  == Master Transmit DMA ==
  App -> HAL_I2C: HAL_I2C_Master_Transmit_DMA(&hi2c2, 0x30F, aTxBuffer, 128)


  == Master Receive DMA ==
  App -> HAL_I2C: HAL_I2C_Master_Receive_DMA(&hi2c2, 0x30F, aRxBuffer, 128)


  == Slave Receive DMA ==
  App -> HAL_I2C: HAL_I2C_Slave_Receive_DMA(&hi2c2, aRxBuffer, 128)



  == Slave Transmit DMA ==
  App -> HAL_I2C: HAL_I2C_Slave_Transmit_DMA(&hi2c2, aTxBuffer, 128)


  @enduml

Code snippet

HAL1

HAL2

#define I2C_ADDRESS          0x30F;
HAL_I2C_Master_Transmit_DMA(&hi2c2, 0x30F, aTxBuffer, 128);

HAL_I2C_Master_Receive_DMA(&hi2c2, 0x30F,aRxBuffer, 128);

HAL_I2C_Slave_Receive_DMA(&hi2c2, aRxBuffer, 128);

HAL_I2C_Slave_Transmit_DMA(&hi2c2, aTxBuffer, 128);
#define I2C_ADDRESS          0x30F;
HAL_I2C_MASTER_Transmit_DMA(&hi2cx, 0x30F, TxBuffer, NumData);

HAL_I2C_MASTER_Receive_DMA(&hi2cx, I2C_SlaveAddress[Adr_Idx], RxBuffer, NumData);

HAL_I2C_SLAVE_Receive_DMA(&hi2cx, RxBuffer, NumData);

HAL_I2C_SLAVE_Transmit_DMA(&hi2cx, TxBuffer, NumData);

Key changes

  1. HAL2 uses renamed I2C DMA APIs (for example, HAL_I2C_MASTER_Transmit_DMA() instead of HAL_I2C_Master_Transmit_DMA()).

Related examples in HAL2

Example name

Brief

example_hal_i2c_two_boards_com_dma_controller

How to handle I2C data buffer transmission/reception between two boards in DMA mode.


Use case: I2C master transmit/receive in interrupt mode

This use case shows how to perform master transmit and master receive operations using interrupts.

Sequence diagram

@startuml
  participant "User Application" as App
  participant "HAL I2C" as HAL_I2C

  == Master Transmit IT ==
  App -> HAL_I2C: HAL_I2C_Master_Transmit_IT(&hi2c2, 0x30F, aTxBuffer, TXBUFFERSIZE)

  == Master Receive IT ==
  App -> HAL_I2C: HAL_I2C_Master_Receive_IT(&hi2c2, I2C_ADDRESS, aRxBuffer, RXBUFFERSIZE)

  @enduml

Code snippet

HAL1

HAL2

#define I2C_ADDRESS          0x30F;

HAL_I2C_Master_Transmit_IT(&hi2c2, 0x30F, aTxBuffer, TXBUFFERSIZE);

HAL_I2C_Master_Receive_IT(&hi2c2, I2C_ADDRESS, aRxBuffer, RXBUFFERSIZE);
uint16_t NumData = 0;

HAL_I2C_MASTER_Transmit_IT(&hi2cx, I2C_SlaveAddress[Adr_Idx], TxBuffer, 0);

HAL_I2C_MASTER_Receive_IT(&hi2cx, I2C_SlaveAddress[Adr_Idx], RxBuffer, 0);

Key changes

  1. HAL2 uses renamed I2C interrupt APIs (for example, HAL_I2C_MASTER_Transmit_IT() instead of HAL_I2C_Master_Transmit_IT()).

Related examples in HAL2

Example name

Brief

example_hal_i2c_two_boards_com_it_controller

How to handle I2C data buffer transmission/reception between two boards, using an interrupt.


Use case: I2C slave transmit in interrupt mode

This use case shows how to perform a slave transmit operation using interrupts.

Sequence diagram

@startuml
  participant "User Application" as App
  participant "HAL I2C" as HAL_I2C

  == Slave Transmit IT ==
  App -> HAL_I2C: HAL_I2C_Slave_Transmit_IT(&hi2c2, aTxBuffer, TXBUFFERSIZE)

  @enduml

Code snippet

HAL1

HAL2

HAL_I2C_Slave_Transmit_IT(&hi2c2, aTxBuffer, TXBUFFERSIZE);
HAL_I2C_SLAVE_Transmit_IT(&hi2cx, TxBuffer, NumData);

Key changes

  1. HAL2 uses renamed I2C interrupt APIs (for example, HAL_I2C_SLAVE_Transmit_IT() instead of HAL_I2C_Slave_Transmit_IT()).

Related examples in HAL2

Example name

Brief

example_hal_i2c_two_boards_com_it_controller

How to handle I2C data buffer transmission/reception between two boards, using an interrupt.


Use case: I2C slave receive in interrupt mode

This use case shows how to perform a slave receive operation using interrupts.

Sequence diagram

@startuml
  participant "User Application" as App
  participant "HAL I2C" as HAL_I2C

  == Slave Receive IT ==
  App -> HAL_I2C: HAL_I2C_Slave_Receive_IT(&hi2c2, aRxBuffer, RXBUFFERSIZE)

  @enduml

Code snippet

HAL1

HAL2

HAL_I2C_Slave_Receive_IT(&hi2c2, aRxBuffer, RXBUFFERSIZE);
HAL_I2C_SLAVE_Receive_IT(&hi2cx, RxBuffer, NumData);

Key changes

  1. HAL2 uses renamed I2C interrupt APIs (for example, HAL_I2C_SLAVE_Receive_IT() instead of HAL_I2C_Slave_Receive_IT()).

Related examples in HAL2

Example name

Brief

example_hal_i2c_two_boards_com_it_controller

How to handle I2C data buffer transmission/reception between two boards, using an interrupt.